package com.whynot.frame.manythread;

/**
 * volatile设置变量，全局的，针对所有线程都是透明的
 * @author Administrator
 *
 */
public class VolatileThread extends Thread{
	
	private volatile Boolean flag = true;

	public Boolean getFlag() {
		return flag;
	}

	public void setFlag(Boolean flag) {
		this.flag = flag;
	}

	@Override
	public void run() {
		while (flag == true) {
			//注意，只要在工作线程里使用了变量，那么就会重新同步主内存的变量
			System.out.println(flag);
		}
		System.out.println("线程已执行完");
	}
	
	public static void main(String[] args) throws InterruptedException {
		VolatileThread t = new VolatileThread();
		t.start();
		Thread.sleep(3000);
		t.setFlag(false);
		System.out.println("flag已设置为false"+t.flag);
		Thread.sleep(1000);
		System.out.println(t.flag);
		
	}
	
	
}
